//
//  SingerSongListViewController.m
//  GSG
//
//  Created by dllo on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SingerSongListViewController.h"
#import"SongBookCell.h"
#import "AFNetworking.h"
#import "AFNetworkingTool.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "PlayerViewController.h"
#import "SongModel.h"
#import "MBProgressHUD.h"
@interface SingerSongListViewController ()
{
   NSInteger number;
    NSUInteger temp;
}
@property(nonatomic,retain)UITableView *tableview;
@property(nonatomic,retain)NSMutableArray *arr;//装传过来的歌曲
@property(nonatomic ,retain)NSMutableDictionary*dic;
@property(nonatomic,retain)NSMutableArray *arrModel;

@end

@implementation SingerSongListViewController

- (void)dealloc
{
    [_tableview  release];
    [_arr release];
    [_dic release];
    [_arrModel release];
    [super dealloc];
}
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        number = 1;
        temp = 1;
        self.arr=[[NSMutableArray alloc]init];
         self.dic=[[NSMutableDictionary alloc]init];
        self.arrModel=[[NSMutableArray alloc]init];

    }

    return self;
}

- (void)data
{
    NSString *str1=[NSString stringWithFormat: @"http://api.dongting.com/song/singer/%@/songs?page=",self.music.singer_id];
//    NSLog(@"%@", str1);
    if (number <= temp) {
        NSString *str2 = [NSString stringWithFormat:@"%ld&size=50", number];
        NSString *str = [NSString stringWithFormat:@"%@%@", str1, str2];
        
    [AFNetworkingTool NetWorkingTooStr:str block:^(id result) {
        NSMutableArray*arr1=[NSMutableArray array];
        arr1=[result objectForKey:@"data"];
        _dic=result;
        temp = [[_dic objectForKey:@"pageCount"] integerValue];
         for (int i=0; i<arr1.count; i++) {
            [self.arr addObject:arr1[i]];
        }

               for (NSInteger i = 0; i <arr1.count ; i++) {
            SongModel *mo = [[SongModel alloc]init];
            NSMutableDictionary *dic1 = arr1[i];
            mo.song_name=[dic1 objectForKey:@"name"];
            mo.singer_name=[dic1 objectForKey:@"singerName"];
            NSMutableArray *arr = [dic1 objectForKey:@"urlList"];
            
            for (NSDictionary *dic  in arr) {
                mo.url=[dic objectForKey:@"url"];
            }
            [self.arrModel addObject:mo];
            [mo release];
        }
        [self.tableview reloadData];
         [_HUD hide:YES ];
    }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing.png"]];
    self.tableview=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.tableview.rowHeight=100;
    self.tableview.backgroundColor = [UIColor clearColor];
    [self.view addSubview: self.tableview ];
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    [self data];
      [self addFooter];
    
    
    [_tableview release];
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    //_HUD.delegate = self;
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor blackColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;
   
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse=@"reuse";
    
    SongBookCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if(!cell){
    
        cell=[[[SongBookCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
        
    
    }
//    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,[self.arr[indexPath.row] objectForKey:@"name"]];
//    cell. singer_nameLabel.text=[self.arr[indexPath.row] objectForKey:@"singerName"];
//    cell.song_nameLabel.text=str;
    
    SongModel *song=self.arrModel[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,song.song_name];
    
    cell. singer_nameLabel.text=song.singer_name;
    cell.song_nameLabel.text=str;
    
    
    
    
    return  cell;

}


-(void)addFooter {
    [self.tableview addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
           
            
             number++;
            [self data];
            [self.tableview reloadData];
            
         [self.tableview footerEndRefreshing];
        });
    }];
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayerViewController *pvc=[[PlayerViewController alloc]init];
    [self.navigationController pushViewController:pvc animated:YES];
    pvc.num=indexPath.row;
    pvc.arr=self.arrModel;
    
    
    [pvc release];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
